sap.ui.define([
		"sap/ui/base/ManagedObject",
		"sap/ui/core/Fragment",
		"sap/ui/core/syncStyleClass",
		"sap/ui/model/Filter",
		"sap/ui/model/FilterOperator",
		"sap/m/MessageToast"
	],
	function(ManagedObject, Fragment, syncStyleClass, Filter, FilterOperator, MessageToast) {
		"use strict";

		return ManagedObject.extend("com.wp.SWIFTSAP.controller.ForwardDialog", {
			constructor: function(oView, oBtn, oThis) { //Othis is detail controller
				this._oView = oView;
				this._oBtn = oBtn;
				this._oThis = oThis;
				this.getForwardUser();
			},
			exit: function() {
				delete this._oView;
			},

			getForwardUser: function() {
				var oViewModel = this._oThis.getModel("detailView");

				var Filters = [];
				var fAppid = new sap.ui.model.Filter("ApplevelDesc1", sap.ui.model.FilterOperator.EQ, this._oThis.vAppid);
				Filters.push(fAppid);
				var fApplevel = new sap.ui.model.Filter("Applevel", sap.ui.model.FilterOperator.EQ, this._oThis.vApplevel);
				Filters.push(fApplevel);

				this._oThis.getModel().read("/ApproverSet", {
					filters: [Filters],
					success: function(oData) {
						oViewModel.setProperty("/forwardUser", oData.results);
					},
					error: function(oData) {
						oViewModel.setProperty("/forwardUser", "");
						this._oThis.showErrorMessage(oData.message);
					}.bind(this)
				});

			},

			onSearch: function(oEvent) {
				var sValue = oEvent.getParameter("value");
				var oFilter = new Filter("Name1", FilterOperator.Contains, sValue);
				var oBinding = oEvent.getParameter("itemsBinding");
				oBinding.filter([oFilter]);
			},

			onDialogClose: function(oEvent) {
				var aContexts = oEvent.getParameter("selectedContexts");
				if (aContexts && aContexts.length) {

					var selectApprovers = aContexts.map(function(oContext) {
						return oContext.getObject().Sapid;
					}).join(",");
					//MessageToast.show("You have chosen " + selectApprovers);
					this.onOk(selectApprovers);

				} else {
					this._oBtn.setEnabled(true);
					MessageToast.show("未选择转发人");
				}
				oEvent.getSource().getBinding("items").filter([]);
			},

			onOk: function(selectApprovers) {
				var odetailThis = this._oThis;
				var oModel = odetailThis.getView().getModel();
				var Requestid = odetailThis.vRequestid; //oThis.getView().byId("requestid").getText();
				var Approverid = odetailThis.getOwnerComponent().getModel("user").getProperty("/Userid");
				var Language = odetailThis.getOwnerComponent().getModel("user").getProperty("/Language");

				var Comments = odetailThis.getView().byId("comments").getValue();
				var sPath = "/RequestSet('" + Requestid + "')/Applevel";
				var Applevel = odetailThis.getModel().getProperty(sPath);

				var fSuccessCallback = function() {
					odetailThis.onCloseDetailPress();
				};

				oModel.callFunction(
					"/ProcessRequest", {
						method: "POST",
						urlParameters: {
							Requestid: Requestid,
							Approverid: Approverid,
							Applevel: Applevel,
							Language: Language,
							Sendbacklevel: "",
							Action: "FORWARD",
							Comments: Comments,
							Forwardapprovers: selectApprovers
						},
						success: function(oData) {
							this._oBtn.setEnabled(true);
							if (oData.Type === "S") {
								this._oView.byId("forwardDialog").close();
								odetailThis.showSuccessMessage(odetailThis.getResourceBundle().getText("processRequestSuccess"), fSuccessCallback);
							} else {
								odetailThis.showErrorMessage(odetailThis.getResourceBundle().getText("processRequestFail", [oData.Message]));
							}
						}.bind(this),
						error: function(oError) {
							this._oBtn.setEnabled(true);
							odetailThis.showErrorMessage(oError.responseText);
						}.bind(this)
					});
			},

			open: function() {
				var oView = this._oView;
				//如果没有相应Dialog，就调用sap.ui.xmlfragment实例化Fragment
				if (!oView.byId("forwardDialog")) {
					Fragment.load({
						id: oView.getId(),
						name: "com.wp.SWIFTSAP.view.Forward",
						controller: this
					}).then(function(oDialog) {
						oView.addDependent(oDialog);
						syncStyleClass(oView.getController().getOwnerComponent().getContentDensityClass(), oView, oDialog);
						oDialog.setResizable(true);
						oDialog.setMultiSelect(true);
						oDialog.open();
					});
				} else {
					oView.byId("forwardDialog").open();
				}
			},
			onCancelDialog: function() {
				this._oBtn.setEnabled(true);
			}
		});
	});